<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class GoodsController extends Controller
{
     public function index($id)
     {

           $res = DB::table('category') -> where('pid',$id) -> get();
           $arr = [];
           foreach ($res as $key => $value) {
               $arr[] .= $value -> id;
           }
           if(!empty($arr))
           {
               $data = DB::table('goods') -> whereIn('pid',$arr) -> get();
           }else
           {
               $data = DB::table('goods') -> where('pid',$id) -> get();
           }



           return view('home.goods.index',['title' =>'商品列表','data' => $data]);
     }


     public function  detail($id)
     {
          $data = DB::table('goods') -> where('gid',$id) -> first();
          $text = DB::table('goods_info') -> where('pid',$id) ->first();

          $com = '';
          $com = DB::table('comment') -> where('gid',$id) ->get();
          // $hao = DB::table('comment') -> select() -> get();

          $leng = 0;
          $num = 0;
          foreach($com as $row)
          {
              $num ++;
              if($row -> comstatus == 0)
              {

                $leng += 5;
              }
              if($row -> comstatus == 1)
              {

                $leng += 3;
              }
              if($row -> comstatus == 3)
              {

                $leng += 1;
              }

          }


          return view('home.goods.detail',['title' => '商品详情','data' => $data,'text'=>$text,'com'=>$com,'num'=>$num,'leng'=>$leng]);
     }

     public function select(Request $request)
     {

          $name = $request -> gname;
          //删除搜索keyword
          $request->flash();
          $data = DB::table('goods') -> where('gname', 'like', '%'.$name.'%') -> get();
          if($data)
          {
                return view('home.goods.index',['title' =>'商品列表','data' => $data]);
          }else
          {
                return back() ;
          }
     }

     public function comment($oid,$gid)
     {
          $res = DB::table('comment') -> where('oid',$oid) -> where('gid',$gid) -> first();
          // dd($res);
          if(empty($res))
          {
            $pic = DB::table('goods') -> where('gid',$gid) -> first() ->gpic;
            return view('home.goods.comment',['title'=>'商品评论','pic'=>$pic,'oid'=>$oid,'gid'=>$gid]);
          }else
          {
            return back() -> with(['info' => '已经评论过该商品了']);
          }

     }


     public function comtext(Request $request)
     {


             $data = $request -> except('_token');
             $time = Date('Y-m-d H-i-s');
             $data['created_at'] = $time;

             $res = DB::table('comment') -> insert( [$data]);

             if($res)
             {
                return redirect('/home/user/userinfo') -> with(['info' => '添加成功']);
             }else
             {
                return back() -> with(['info' => '添加失败']);
             }


     }



    public function shoucang($id)
    {
        if(session('user_info'))
      {
            $isshoucang =DB::table('shoucang') -> where([['gid','=',$id],['uid','=',Session('user_info') -> id]]) -> first();
            if($isshoucang)
            {
               return back() -> with(['info' => '您已添加收藏']);
            }
            else{
                $shoucang['uid'] = Session('user_info') -> id;
                $shoucang['gid'] = $id;
                $time = Date('Y-m-d H-i-s');
                $shoucang['created_at'] = $time;
                $shoucang['updated_at'] = $time;
                $res = DB::table('shoucang') ->insert([$shoucang]);
            }
        if($res)
        {
            return back() -> with(['info' => '添加收藏成功']);
        }else
        {
            return back() -> with(['info' => '添加收藏失败']);
        }

      }else
      {
          return view('home.user.login',['title'=>'用户登录']);
      }
    }




}
